<?php

namespace app\roomcall\controller;

use app\common\model\Orderproject;
use app\common\service\OrdinaryService;
use app\common\service\ReceiverService;
use Think\Db;
use think\Exception;
use think\Validate;

class Engineer extends Home
{
    public $noNeedLogin = [];

    /**
     * 获取技师列表
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['limit'])) {
                $getData['limit'] = '10';
            }
            if (empty($getData['store_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_users')
                ->field('id,work_number,work_status')
                ->where(['role' => '3', 'store_id' => $getData['store_id']])
                ->paginate($getData['limit']);
            $this->result('', $data, 200);
        }
        $this->result('无效的请求方式', '', 204);
    }

    /**
     * 搜索技师
     */
    public function search()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['store_id']) || empty($getData['work_number'])) {
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_users')
                ->field('id,work_number,work_status')
                ->where([
                    'work_number' => $getData['work_number'],
                    'store_id' => $getData['store_id'],
                    'status'=>0
                ])
                ->find();
            $this->result('', $data, 200);
        }
        $this->result('无效的请求方式', '', 204);
    }


    /**
     * 技师排队
     */
    public function lineup()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $rule = [
                'store_id|门店' => 'require|number',
                'engineer_id|技师' => 'require|number',
                'hand_card_id|手牌' => 'require|number',
                'room_id|房间' => 'require|number',
                'project_id|项目' => 'require|number',
            ];
            // 定义验证提示
            $message = [
                'store_id.require' => '门店不能为空',
                'store_id.number' => '门店格式不正确',
                'engineer_id.require' => '技师不能为空',
                'engineer_id.number' => '技师格式不正确',
                'hand_card_id.require' => '手牌不能为空',
                'hand_card_id.number' => '手牌格式不正确',
                'room_id.require' => '房间不能为空',
                'room_id.number' => '房间格式不正确',
                'project_id.require' => '项目不能为空',
                'project_id.number' => '项目格式不正确',

            ];
            $validate = new Validate($rule, $message);
            if (!$validate->check($data)) {
                $this->result($validate->getError(), '', 204);
            }
            //插入数据
            $data['create_time'] = time();
            $find = Db::name('qt_lineup')
                ->where(['engineer_id' => $data['engineer_id']])
                ->whereTime('create_time', 'today')
                ->order('id desc')
                ->find();
            //如果前面有排队的时间在原有基础上加十分钟否则就是十分钟
            if (empty($find)) {
                $estimate_time = '10';
            } else {
                $time = Db::name('qt_project')->where('id', $data['project_id'])->value('time');
                $estimate_time = $find['estimate_time'] + 10 + $time;
            }
            $data['estimate_time'] = $estimate_time;
            $save = Db::name('qt_lineup')->insert($data);
            if ($save) {
                $this->result('排队成功', '', 200);
            }
            $this->result('排队失败', '', 204);

        }
        $this->result('无效的请求方式', '', 204);
    }


    /**
     * 预约列表
     */
    public function list_appointment()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $data = Db::name('qt_room_appointment')
                ->field('ra.id,ra.time,ra.is_call,r.name as room_name,
                p.name as project_name,engineer_id,ra.delay,ra.content,ra.room_id,
                ra.status,ra.creater')
                ->alias('ra')
                ->leftJoin('qt_room r', 'ra.room_id = r.id')
                ->leftJoin('qt_project p', 'ra.project_id = p.id')
                ->whereIn('ra.status', ['1'])
                ->where('ra.store_id', $userInfo['store_id'])
                ->where('ra.engineer_id', $userInfo['id'])
                ->select();
            foreach ($data as $key => $value) {
                $data[$key]['engineer'] = Db::name('qt_users')->where('id', $value['engineer_id'])->value('work_number');
                $data[$key]['creater_name'] = Db::name('qt_users')->where('id', $value['creater'])->value('real_name');
                unset($data[$key]['engineer_id']);
            }
            $this->result('查询成功', $data, 200);
        }
    }



    /**
     * 房间预约
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function appointment()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!empty($getData['room_id'])){
                $roomRes = Db::name('qt_room')->where('id',$getData['room_id'])->find();
            }else{
                $roomRes['status_id'] = '';
            }

            $getData['created_time'] = time();
            $getData['time'] = strtotime($getData['time']);
            $getData['creater'] = $this->userInfo['id'];
            Db::startTrans();
            try {
                //修改订单状态
                Db::name('qt_room_appointment')->insert($getData);
                if($roomRes['status_id'] == '1'){
                    Db::name('qt_room')->where('id', $getData['room_id'])->update(['status_id' => '9']);
                }
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->result('预约失败', '', 204);
            }
            $this->result('预约成功', '', 200);
        }
    }


    /**
     * 取消预约
     */
    public function del_appointment()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['id'])) {
                $this->result('缺少预约信息', '', 204);
            }
            $find = Db::name('qt_room_appointment')->where('id', $getData['id'])->find();
            $userStatus = Db::name('qt_users')->where('id', $find['engineer_id'])->value('status_id');
            if (empty($find)) {
                $this->result('未找到房间预约信息', '', 204);
            }
            Db::startTrans();
            try {
                //修改订单状态
                Db::name('qt_room_appointment')->where('id', $getData['id'])->update(['status' => '3']);
                Db::name('qt_room')->where('id', $find['room_id'])->update(['status_id' => '1']);
                if($userStatus == '3'){
                    Db::name('qt_uses')->where('id', $find['engineer_id'])->update(['status_id' => '1']);
                }
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->result('取消失败', '', 204);
            }
            $this->result('取消成功', '', 200);
        }
    }


    /**
     * 技师会做的项目
     */
    public function get_project()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['engineer_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_user_project')
                ->field('up.project_id,p.name as project_name')
                ->alias('up')
                ->leftJoin('qt_project p', 'up.project_id = p.id')
                ->where('up.engineer_id', $getData['engineer_id'])
                ->select();
            $this->result('查询成功', $data, 200);

        }
        $this->result('无效的请求', '', 204);
    }




}
